package com.youdianstar.app.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;

import java.util.ArrayList;
import java.util.List;

/**
 * File: MapJumpUtil
 * Author: 82149 Create: 2021/12/17 19:56
 * Changes (from 2021/12/17)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 */
public class MapJumpUtil {
    public static boolean hasApp(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        List<String> packageNames = new ArrayList<>();
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        return packageNames.contains(packageName);
    }

    public static void jumpToGoogleMap(Context context, Double latitude, Double longitude) {
        if (null == latitude || null == longitude){
            return;
        }
        Uri gmmIntentUri = Uri.parse("http://maps.google.com/maps?"
                + "&daddr=" + (latitude) + "," + longitude
                + "&avoid=w");
        Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
        //mapIntent.setPackage("com.google.android.apps.maps");
        mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
        context.startActivity(mapIntent);
    }

    public static void jumpToGoogleMap(Context context, String address) {
        Uri gmmIntentUri = Uri.parse("http://maps.google.com/maps?"
                + "&daddr=" + address + "&avoid=w");
        Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
        //mapIntent.setPackage("com.google.android.apps.maps");
        mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
        context.startActivity(mapIntent);
    }
}
